<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <code-tech.diandian.com>
// +----------------------------------------------------------------------

namespace Help\Model;

use Think\Model;

/**
 * Class HelpModel  参加帮忙模型
 * @package Help\Model
 * @author:xjw129xjt xjt@ourstu.com
 */
class HelpAttendModel extends Model
{

    protected $tableName = 'help_attend';

    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_BOTH),
        array('status', '0', self::MODEL_INSERT),
    );

    protected $insertFields = 'help_id,phone,telephone,qq,weixin,hidden'; // 新增数据的时候允许写入name和email字段
    protected $updateFields = 'help_id,phone,telephone,qq,weixin,hidden,status'; // 编辑数据的时候只允许写入email字段

    public function signHelp($data)
    {
        $data = $this->create($data);
        $data['uid'] = is_login();
        $res = $this->add($data);
        return $res;
    }

    public function deleteAttend($map){
        $map['uid']=is_login();
        $status=$this->where($map)->field('status')->find();
        $res['status']=0;
        if($status['status']){
            $res['info']='你的报名已被接受，不能取消';
        }else{
            $result=$this->where($map)->delete();
            if($result){
                $res['status']=1;
            }else{
                $res['info']='取消报名失败';
            }
        }
        return $res;
    }
    public function getAllSignListByHelpId($help_id)
    {

        $sign_list = $this->where(array('help_id' => $help_id, 'status' => 0))->select();
        foreach ($sign_list as &$v) {
            $v['user'] = query_user(array('uid', 'username', 'nickname', 'space_url', 'space_link', 'avatar64', 'avatar128', 'rank_html', 'signature','address','doing','fans'), $v['uid']);
            $v['user']['help_count'] = $this->getHelpCountByUid($v['uid']);
        }
        unset($v);
        return $sign_list;
    }

    public function getShowSignListByHelpId($help_id)
    {

        $sign_list = $this->where(array('help_id' => $help_id, 'status' => 0, 'hidden' => 0))->select();
        empty($sign_list) && $sign_list = array();
        //如果自己发布的是隐藏的，则显示出来
        $my = array();
        if (is_login()) {
            $my = $this->where(array('help_id' => $help_id, 'status' => 0, 'hidden' => 1, 'uid' => is_login()))->select();
            empty($my) && $my = array();
        }
        $sign_list = array_merge($sign_list, $my);
        if ($sign_list) {
            foreach ($sign_list as &$v) {
                $v['user'] = query_user(array('uid', 'username', 'nickname', 'space_url', 'space_link', 'avatar64', 'avatar128', 'rank_html', 'signature','address','doing','fans'), $v['uid']);
                $v['user']['help_count'] = $this->getHelpCountByUid($v['uid']);
            }
            unset($v);
        }
        return $sign_list;
    }

    public function getHiddenCountByHelpId($help_id)
    {
        $hidden_count = $this->where(array('help_id' => $help_id, 'status' => 0, 'hidden' => 1))->count();
        //如果机子发布的是隐藏的，则显示自己的，count-1.
        if (is_login()) {
            if ($this->where(array('help_id' => $help_id, 'status' => 0, 'hidden' => 1, 'uid' => is_login()))->find()) {
                $hidden_count--;
            }
        }
        return $hidden_count;
    }

    public function getHelpCountByUid($uid = 0)
    {
        $uid = $uid == 0 ? is_login() : $uid;
        $help = $this->where(array('uid' => $uid, 'status' => 1))->count();
        return $help;

    }

    public function getAcceptByHelpId($help_id)
    {
        $accept = $this->where(array('help_id' => $help_id, 'status' => 1))->select();
        if ($accept) {
            foreach ($accept as &$v) {
                $v['user'] = query_user(array('uid', 'username', 'nickname', 'space_url', 'space_link', 'avatar64', 'avatar128', 'rank_html', 'signature','address','doing','fans'), $v['uid']);
                $v['user']['help_count'] = $this->getHelpCountByUid($v['uid']);
            }
            unset($v);
        }
        return $accept;
    }

    public function acceptSomeone($help_id,$uid){
       $res= $this->where(array('help_id'=>$help_id,'uid'=>$uid))->setField('status',1);
        D('member')->where(array('uid'=>$uid))->setInc('help_count');
       clean_query_user_cache($uid,'help_count');
       return $res;
    }

    public function cancelAccept($help_id,$uid){
        $res= $this->where(array('help_id'=>$help_id,'uid'=>$uid))->setField('status',0);
        D('member')->where(array('uid'=>$uid))->setDec('help_count');
        clean_query_user_cache($uid,'help_count');
        return $res;
    }

    public function getAttend($id){
        $attend = $this->where(array('id'=>$id))->find();
        return $attend;
    }
    public function getAttendByUid($uid){
        $attend = $this->where(array('uid'=>$uid,'status'=>1))->field('help_id')->select();
        $ids = getSubByKey($attend,'help_id');
        $list = array();
        foreach($ids as $v){
            $list[] = D('HelpWorks')->getWorks($v);
        }
        unset($v);
        return $list;
    }

    public function getAttendHelpByUid($uid,$page=1,$limit=10){
        $attend = $this->where(array('uid'=>$uid,'status'=>1))->field('help_id')->page($page,$limit)->select();
        $ids = getSubByKey($attend,'help_id');
        $list = array();
        foreach($ids as $v){
            $list[] = D('Help')->getHelp($v);
        }
        unset($v);
        return $list;
    }

    public function getAllCountByHelpId($help_id){
        $sign_list = $this->where(array('help_id' => $help_id))->count();
        return $sign_list;
    }

    public function getAcceptCount($help_id){
        $accept = $this->where(array('help_id' => $help_id, 'status' => 1))->count();
        return $accept;
    }

    public function getListFriendsAttend($uids=array(),$r=100){
        $attend_list=S('attend_list_friends_'.is_login());
        if(!$attend_list){
            $attend_list = $this->where(array('uid'=>array('in',$uids)))->limit($r)->order('create_time desc')->select();
            foreach($attend_list as &$val){
                $val['user'] = query_user(array('uid', 'nickname', 'space_url', 'avatar64', 'avatar128', 'rank_html', 'address', 'doing'), $val['uid']);
                $val['help']=D('Help/Help')->getHelp($val['help_id']);
            }
            unset($val);
            S('attend_list_friends_'.is_login(),$attend_list,60);
        }
        return $attend_list;
    }
}